<template>
  <div>
    <div class="search">
      <input type="text" v-model="keyword" placeholder="请输入城市名或拼音" class="search-input">
    </div>
    <div class="search-content" ref="search" v-show="keyword">
      <ul>
        <li class="border-bottom" v-for="item in list" :key="item.id" @click="handleCityClick(item.name)">{{item.name}}</li>
        <li v-show="noDate" class="border-bottom">没有匹配到相关内容~</li>
      </ul>
    </div>
  </div>
</template>
<script>
  import {mapMutations} from 'vuex'
  import BScroll from 'better-scroll'
  export default {
    name: 'CitySearch',
    props:{
      cities:Object
    },
    computed:{
      noDate () {
        return !this.list.length
      }
    },
    data () {
      return {
        keyword:'',
        list:[],
        timer:null
      }
    },
    watch: {
      keyword () {
        if(this.timer){
          clearTimeout(this.timer)
        }
        if(!this.keyword){
          this.list= []
          return
        }
        this.timer = setTimeout(()=>{
          const reslut=[]
          for (let i in this.cities) {
            this.cities[i].forEach((value)=>{
              if(value.spell.indexOf(this.keyword) >-1 ||value.name.indexOf(this.keyword)>-1){
                reslut.push(value)
              }
            })
          }
          this.list=reslut
        },30)
      }
    },
    methods:{
      handleCityClick (city) {
        //this.$store.commit("changesCity",city)
        this.changesCity(city)
        this.$router.push('./')
      },
      ...mapMutations(['changesCity'])
    },
    mounted () {
      this.scroll = new BScroll(this.$refs.search)
    }
  }

</script>
<style lang="stylus" scoped>
  @import "../../../../static/css/varibles.styl"
  .search
    width: 100%;
    height: 0.72rem;
    padding: 0 0.1rem
    background: $bgColr
    box-sizing: border-box
    .search-input
      line-height: 0.62rem
      width: 100%
      -webkit-border-radius: 0.06rem
      -moz-border-radius: 0.06rem
      border-radius: 0.06rem
      text-align: center
      color: #666
      font-size: 0.26rem
      -webkit-box-sizing: border-box
      -moz-box-sizing: border-box
      box-sizing: border-box
      padding: 0 0.1rem

  .search-content
    position: absolute
    top: 1.55rem
    left: 0
    right: 0
    bottom: 0
    background: #eee
    z-index: 1
    overflow: hidden
    & ul li
      background: #fff
      height: 0.8rem
      line-height: 0.8rem
      padding-left: 0.2rem
      color: #333
</style>
